package com.egg.ylt.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.egg.ylt.R;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.activity.ACT_ConfirmPayment;
import com.egg.ylt.activity.ACT_MealCardDetails;
import com.egg.ylt.pojo.ShopCardByIdEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ADA_OtherMealCardAdapter extends RecyclerView.Adapter<ADA_OtherMealCardAdapter.OtherMealCarlViewHolder> {

    private Context mContext;   //添加数据
    private List<ShopCardByIdEntity.CardListEntity> mList;

    private String mShopId;
    private String mOpenTime;

    public ADA_OtherMealCardAdapter(String shopId, String openTime) {
        this.mList = new ArrayList<>();
        mShopId = shopId;
        mOpenTime = openTime;
    }

    public void setData(List<ShopCardByIdEntity.CardListEntity> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public OtherMealCarlViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        return new OtherMealCarlViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_other_meal_card,
                parent, false));
    }

    @Override
    public void onBindViewHolder(OtherMealCarlViewHolder holder, int position) {
        holder.refreshData(position);
    }


    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    class OtherMealCarlViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.image_iv)
        ImageView mImageIv;
        @BindView(R.id.meal_card_name_tv)
        TextView mMealCardNameTv;
        @BindView(R.id.price_tv)
        TextView mPriceTv;
        @BindView(R.id.old_price_tv)
        TextView mOldPriceTv;

        @BindView(R.id.sold_count_tv)
        TextView mSoldCountTv;

        @BindView(R.id.order_now_tv)
        TextView mOrderNowTv;


        public OtherMealCarlViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void refreshData(int position) {
            final ShopCardByIdEntity.CardListEntity dataEntity = mList.get(position);

            Glide.with(mContext).load(dataEntity.getImageUrl()).placeholder(R.mipmap.ic_serve_default).error(R.mipmap.ic_serve_default).dontAnimate().into(mImageIv);

            mMealCardNameTv.setText(dataEntity.getName());

            mPriceTv.setText(dataEntity.getPrice());

            mOldPriceTv.setText("¥" + dataEntity.getTotalPrice());

            CustomUtils.setMiddleLine(mOldPriceTv);

            mSoldCountTv.setText("已售" + dataEntity.getSellNum());

            mOrderNowTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ACT_ConfirmPayment.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("cardId", dataEntity.getId());
                    bundle.putString("cardName", dataEntity.getName());
                    bundle.putString("price", dataEntity.getPrice());
                    bundle.putString("endDate", dataEntity.getEndDate());
                    bundle.putString("shopName", dataEntity.getShopName());
                    bundle.putString("cardImageUrl", dataEntity.getImageUrl());
                    bundle.putString("applyType", "2");
                    bundle.putString("shopId", mShopId);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            });


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ACT_MealCardDetails.class);
                    intent.putExtra("cardId", dataEntity.getId());
                    intent.putExtra("shopId", mShopId);
                    intent.putExtra("shopName", dataEntity.getShopName());
                    intent.putExtra("openTime", mOpenTime);
                    intent.putExtra("cardImageUrl", dataEntity.getImageUrl());
                    mContext.startActivity(intent);
                }
            });
        }
    }
}
